//
//  FoundRmHeaderView.swift
//  KkmhSwift
//  发现推荐头部view
//  Created by tenltrs on 2018/11/14.
//  Copyright © 2018 Tenltrs. All rights reserved.
//

import UIKit

class FoundRmHeaderView: UIView,SDCycleScrollViewDelegate {
    
    private var bannerView : SDCycleScrollView!
    
    private var lastUrlArr : [String]?
    
    private var _bannerModel : FoundRmInfoModel?
    var bannerModel : FoundRmInfoModel?{
        set{
            _bannerModel = newValue
            self.reloadBanner()
        }
        get{
            return _bannerModel
        }
    }
    
    private var _itemModel : FoundRmInfoModel?
    var itemModel : FoundRmInfoModel?{
        set{
            _itemModel = newValue
            self.reloadItem()
        }
        get{
            return _itemModel
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.white
        var ratio : CGFloat = 750.0/515
        if iPhoneX{
            ratio = 1280.0/1070
        }
        bannerView=SDCycleScrollView.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenWidth/ratio), delegate: self, placeholderImage: nil)
        bannerView.autoScrollTimeInterval=5
        bannerView.backgroundColor=UIColor.BackgroundColor
        bannerView.pageControlBottomOffset=10
        bannerView.bannerImageViewContentMode=UIView.ContentMode.scaleAspectFill
        self.addSubview(bannerView)
        
        self.height = bannerView.frame.maxY+90
        
    
        let maskView = UIImageView.init(image: UIImage(named: "ic_banner_arc_375x10_"))
        self.addSubview(maskView)
        maskView.snp.makeConstraints { (make) in
            make.bottom.equalTo(bannerView.snp.bottom)
            make.left.right.equalToSuperview()
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func reloadBanner() {
        if (bannerModel?.banners == nil) {
            return
        }
        var imageArr : [String] = []
        for item in (bannerModel?.banners)! {
            imageArr.append(item.pic!)
        }
        bannerView.imageURLStringsGroup = imageArr
    }
    
    func reloadItem() {
        
        if (itemModel?.banners == nil) {
            return
        }
        
        var imageArr : [UIImageView] = []
        for (index,item) in (itemModel?.banners)!.enumerated() {
            
            let imageView = UIImageView().then { (make) in
                make.tag = 100+index
                make.ts_loadImage(url: item.pic)
                make.contentMode=UIView.ContentMode.scaleAspectFit
            }
            self.addSubview(imageView)
            imageArr.append(imageView)
        }
        imageArr.snp.makeConstraints { (make) in
            make.centerY.equalTo(bannerView.snp.bottom).offset(45)
            make.height.equalTo(65)
        }
        
        imageArr.snp.distributeViewsAlong(axisType: NSLayoutConstraint.Axis.horizontal, fixedSpacing: 0)
    }
}
